package com.lichongbing.lyoggl.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lichongbing.lyoggl.model.Tags;
import com.lichongbing.lyoggl.service.LyogglUsersService;
import com.lichongbing.lyoggl.service.TagsService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/4/7 3:23 下午
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/tags")
public class TagsController {

    @Autowired
    private TagsService tagsService;
    @Autowired
    private LyogglUsersService lyogglUsersService;



    /**
     * 根据用户id查询所有的
     * @param request
     * @return
     */

    @PostMapping("/findAll")
    public JSONObject getTagsByUserId(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            boolean flag = lyogglUsersService.isToken(userid);
            if(flag) {
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", tagsService.getTagsByUserId(userid));
            return jsonObject;}else {
                jsonObject.put("code",402);
                jsonObject.put("msg","token无效");
                return jsonObject;
            }
        }else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     * 新增tags
     * @param jsonObject
     * @param request
     * @return
     */

    @PostMapping("/add")
    public JSONObject addTags(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        JSONObject jsonObject2 = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        String id = UUID.randomUUID().toString().replace("-", "");
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            Tags tags = new Tags();
            tags.setId(id);
            tags.setUid(userid);
            tags.setName(jsonObject.getString("name"));
            tagsService.save(tags);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "添加成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     * 删除tags
     * @param id
     * @param request
     * @return
     */

    @PostMapping("/delete/{id}")
    public JSONObject deleteTags( @PathVariable String id, HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            tagsService.deleteByUserid(id);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "删除成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

    /**
     * 修改tags
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/update")
    public JSONObject updateTags(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        String token = request.getHeader("token");
        Tags tags = JSONObject.toJavaObject(jsonObject, Tags.class);
        boolean expiration = JwtTokenUtils.isExpiration(token);
        if (expiration == false) {
            LambdaUpdateWrapper<Tags> wrapper = Wrappers.<Tags>lambdaUpdate();
            if(!tags.getName().equals("") ){
                 wrapper.set(Tags::getName, tags.getName());
            }
            if(!tags.getColor().equals("") ){
                wrapper.set(Tags::getColor, tags.getColor());
            }
            if(!tags.getR().equals("") ){
                wrapper.set(Tags::getR, tags.getR());
            }
            if(!tags.getG().equals("") ){
                wrapper.set(Tags::getG, tags.getG());
            }
            if(!tags.getB().equals("") ){
                wrapper.set(Tags::getB, tags.getB());
            }
            wrapper.eq(Tags::getId, tags.getId());
            tagsService.update(null, wrapper);
            jsonObject.put("code", 200);
            jsonObject.put("msg", "修改成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 401);
            jsonObject.put("msg", "token失效");
            return jsonObject;
        }
    }

}
